package com.codechuan.demo.ui.myOrder;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;


import com.codechuan.demo.R;
import com.codechuan.demo.adapter.OrderBaseAdapter;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.ui.login.LoginActivity;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.MyHttpUtil;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;

@SuppressLint("StaticFieldLeak")
public class MyOrder extends AppCompatActivity implements View.OnClickListener {
    public static BottomSheetDialog bottomSheetDialog;
    public static TextView textStartAddress;
    public static TextView textEndAddress;
    public static TextView wh;
    public static TextView startDes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_order);

        // 弹出层
        View bottomSheetView = getLayoutInflater().inflate(R.layout.tc, null);
        bottomSheetDialog = new BottomSheetDialog(MyOrder.this);
        bottomSheetDialog.setContentView(bottomSheetView);

        textStartAddress = bottomSheetView.findViewById(R.id.textStartAddress);
        textEndAddress = bottomSheetView.findViewById(R.id.textEndAddress);
        wh = bottomSheetView.findViewById(R.id.wh);
        startDes = bottomSheetView.findViewById(R.id.textView82);
        DialogUtil.showDialog(Constant.context, "获取中,请稍后");
        // 设置订单数量
        TextView orderNumber = findViewById(R.id.orderNumber);
        // 制空避免重复加入
        Constant.ORDER_LIST = new ArrayList<>();

        Constant.executorService.execute(() -> {
            boolean userOrderList = MyHttpUtil.getUserOrderList(Constant.USER_ACCOUNT);
            if (userOrderList) {
                Constant.handler.post(() -> {
                    // 创建自定义适配器对象
                    ListView listView = findViewById(R.id.orderListData);
                    OrderBaseAdapter orderBaseAdapter = new OrderBaseAdapter(Constant.context, Constant.ORDER_LIST, bottomSheetDialog);
                    listView.setAdapter(orderBaseAdapter);
                    String orderNumberString = "执行PK订单数量:" + Constant.ORDER_LIST.size();
                    orderNumber.setText(orderNumberString);
                });
            }
        });


        Button order_jump_home = findViewById(R.id.order_jump_home);
        order_jump_home.setOnClickListener(this);
        Button delete_all_order = findViewById(R.id.delete_all_order);
        delete_all_order.setOnClickListener(this);




        if (Constant.QUERY_ORDER_TYPE.equals("APP")) {
            delete_all_order.setText("删除所有APP订单");
        } else {
            delete_all_order.setText("删除所有小程序订单");
        }

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.order_jump_home) {
            Intent intent = new Intent();
            intent.setClass(this, LoginActivity.class);
            startActivity(intent);
        }
        if (id == R.id.delete_all_order) {
            Constant.executorService.execute(() -> {
                boolean isOk = MyHttpUtil.deleteAllOrderByAccount(Constant.USER_ACCOUNT);
                if (isOk) {
                    Constant.handler.post(() -> DialogUtil.showDialog(Constant.context, "删除成功请返回页面重新刷新"));
                } else {
                    Constant.handler.post(() -> DialogUtil.showDialog(Constant.context, "删除失败."));
                }
            });
        }
    }

    public static void wc(String one, String two, String three, String f) {
        Constant.handler.post(() -> {
            textStartAddress.setText("起点:" + one);
            textEndAddress.setText("终点:" + two);
            wh.setText("乘客手机" + three);
            startDes.setText("您距离乘客:" + f + "公里");
            bottomSheetDialog.show();
        });


    }


}